package cn.blunce.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by Blunce on 2015/11/3 0003.
 */
public class NetHelper {

    public static final int TYPE_WIFI = 0x8001;
    public static final int TYPE_MOBILE = 0x8002;
    public static final int TYPE_EHTERNET = 0x8003;
    public static final int TYPE_DISCONNECT = 0x8004;

    /**
     * Check whether the device is connected, and if so,
     * whether the connection is wifi or mobile (it could be something else).
     * 不能忘记添加android.Manifest.permission.ACCESS_NETWORK_STATE.
     *
     * @param context
     * @return
     */
    private static int checkNetworkConnection(Context context) {

        boolean wifiConnected = false;
        boolean mobileConnected = false;
        boolean ehternetConnected = false;

        ConnectivityManager connMgr =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeInfo = connMgr.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
            mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
            ehternetConnected = activeInfo.getType() == ConnectivityManager.TYPE_ETHERNET;
            if (wifiConnected) {
                return TYPE_WIFI;
            } else if (mobileConnected) {
                return TYPE_MOBILE;
            } else if (ehternetConnected) {
                //以太网连接
                return TYPE_EHTERNET;
            }

        } else {
            return TYPE_DISCONNECT;
        }
        return TYPE_DISCONNECT;
    }
}
